package cn.t.repository.impl.device;

import cn.t.converter.device.FaultConverter;
import cn.t.core.repoistory.RepositoryImpl;
import cn.t.model.device.FaultDO;
import cn.t.persistence.entity.device.FaultPO;
import cn.t.persistence.mapper.device.FaultMapper;
import cn.t.repository.device.FaultRepository;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 设备报警信息表(日志)(Fault)数据仓库
 *
 * @author t
 * @since 2024-05-24 10:18:09
 */
@Slf4j
@Repository
@AllArgsConstructor
public class FaultRepositoryImpl extends RepositoryImpl<FaultMapper, FaultPO, FaultConverter, FaultDO> implements FaultRepository {


    @Override
    public List<FaultDO> findByModel(FaultDO faultDO, int start, Integer pageSize, String field, String direction) {

        String lastSql = StringUtils.hasText(field) ?
                " order by " + field + " " + direction + " limit " + start + "," + pageSize : " limit " + start + "," + pageSize;

        List<FaultPO> list = getWrapper(faultDO).last(lastSql).list();
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }

        return list.stream().map(converter::po2Model).collect(Collectors.toList());
    }

    private LambdaQueryChainWrapper<FaultPO> getWrapper(FaultDO faultDO) {
        LambdaQueryChainWrapper<FaultPO> wrapper = lambdaQuery();
        if (!ObjectUtils.isEmpty(faultDO.getDeviceId())) {
            wrapper.eq(FaultPO::getDeviceId, faultDO.getDeviceId());
        }
        if (!ObjectUtils.isEmpty(faultDO.getCtrlBoardNo())) {
            wrapper.eq(FaultPO::getCtrlBoardNo, faultDO.getCtrlBoardNo());
        }
        if (!ObjectUtils.isEmpty(faultDO.getCargowayNo())) {
            wrapper.eq(FaultPO::getCargowayNo, faultDO.getCargowayNo());
        }
        if (!ObjectUtils.isEmpty(faultDO.getDoorLockNo())) {
            wrapper.eq(FaultPO::getDoorLockNo, faultDO.getDoorLockNo());
        }
        if (!ObjectUtils.isEmpty(faultDO.getFaultCode())) {
            wrapper.eq(FaultPO::getFaultCode, faultDO.getFaultCode());
        }
        if (!ObjectUtils.isEmpty(faultDO.getContent())) {
            wrapper.eq(FaultPO::getContent, faultDO.getContent());
        }
        if (!ObjectUtils.isEmpty(faultDO.getStatus())) {
            wrapper.eq(FaultPO::getStatus, faultDO.getStatus());
        }
        if (!ObjectUtils.isEmpty(faultDO.getGmtModified())) {
            wrapper.between(FaultPO::getGmtModified, faultDO.getGmtModifiedStart(), faultDO.getGmtModifiedEnd());
        }
        if (!ObjectUtils.isEmpty(faultDO.getGmtCreated())) {
            wrapper.between(FaultPO::getGmtCreated, faultDO.getGmtCreatedStart(), faultDO.getGmtCreatedEnd());
        }
        wrapper.in(!CollectionUtils.isEmpty(faultDO.getDeviceIds()), FaultPO::getDeviceId, faultDO.getDeviceIds());
        return wrapper;
    }

    @Override
    public long count(FaultDO faultDO) {
        return getWrapper(faultDO).count();
    }
}
